import matplotlib.pyplot as plt
import numpy as np

def set_chinese_font():
    """设置中文字体"""
    plt.rcParams['font.sans-serif'] = ['SimHei', 'Microsoft YaHei', 'DejaVu Sans']
    plt.rcParams['axes.unicode_minus'] = False  # 解决负号显示问题

set_chinese_font()

def coordinate_geometry():
    # 创建图形
    fig, axes = plt.subplots(1, 2, figsize=(12, 5))
    
    # 笛卡尔坐标系
    x = np.linspace(-5, 5, 100)
    y1 = 2*x + 1
    y2 = -0.5*x + 3
    
    axes[0].plot(x, y1, 'b-', label='y = 2x + 1')
    axes[0].plot(x, y2, 'r-', label='y = -0.5x + 3')
    
    # 找到交点
    A = np.array([[2, -1], [0.5, 1]])
    B = np.array([-1, 3])
    intersection = np.linalg.solve(A, B)
    
    axes[0].plot(intersection[0], intersection[1], 'ro', markersize=8)
    axes[0].text(intersection[0]+0.2, intersection[1], f'({intersection[0]:.1f}, {intersection[1]:.1f})')
    
    axes[0].set_xlim(-5, 5)
    axes[0].set_ylim(-5, 5)
    axes[0].axhline(0, color='black', linewidth=0.5)
    axes[0].axvline(0, color='black', linewidth=0.5)
    axes[0].grid(True)
    axes[0].set_title('笛卡尔坐标系与直线交点')
    axes[0].legend()
    
    # 极坐标系
    ax_polar = fig.add_subplot(122, projection='polar')
    theta = np.linspace(0, 2*np.pi, 100)
    r = 2 * np.sin(3*theta)  # 三叶玫瑰线
    
    ax_polar.plot(theta, r, 'g-')
    ax_polar.set_title('极坐标系: 三叶玫瑰线 r = 2sin(3θ)')
    ax_polar.grid(True)
    
    plt.tight_layout()
    plt.show()

coordinate_geometry()